package com.buli.blmall.admin.core.security.domain;

import com.buli.blmall.admin.common.domain.SysLoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiang.gao
 * @date 2025/3/3 16:23
 */
public class AuthenticationUser implements Authentication {

    private SysLoginUser loginUser;
    private boolean authenticated;
    private List<GrantedAuthority> authorities;

    public AuthenticationUser(SysLoginUser loginUser) {
        authenticated = true;
        this.loginUser = loginUser;
        authorities = loginUser.getPermission().stream().map(menuPermission -> (GrantedAuthority) () -> menuPermission).collect(Collectors.toList());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public Object getCredentials() {
        return loginUser.getToken();
    }

    @Override
    public Object getDetails() {
        return loginUser;
    }

    @Override
    public Object getPrincipal() {
        return loginUser.getPrincipal();
    }

    @Override
    public boolean isAuthenticated() {
        return authenticated;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        this.authenticated = isAuthenticated;
    }

    @Override
    public String getName() {
        return loginUser.getUserDetail().getRealName();
    }
}
